package com.xsprite.managesys.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.game.GMClientServer;
import com.game.ZZServerInfo;
import com.game.gm.config.GMComm;
import com.game.gm.load.GMCommLoad;
import com.xsprite.managesys.bean.GMServer;
import com.xsprite.managesys.bean.Plat;

import game.server.config.ServerConfig;
import game.server.config.ServerInfo;


public class GMCommandManager {
	
	private static GMCommandManager instance;
	
	private GMCommandManager(){
		
	}
	
	public static GMCommandManager getInstance(){
		if(instance == null){
			instance = new GMCommandManager();
		}
		return instance;
	}
	
	public List<GMComm> getGMComms(){
		ConcurrentHashMap<Integer, GMComm> comms = GMCommLoad.getInstance().getGmComms();
		List<GMComm> commsList = new ArrayList<GMComm>();
		for(GMComm comm : comms.values()){
			commsList.add(comm);
		}
		Collections.sort(commsList, sorter);
		return commsList;
	}
	
	public GMComm getGMCommByTag(int tag){
		ConcurrentHashMap<Integer, GMComm> comms = GMCommLoad.getInstance().getGmComms();
		for(Entry<Integer, GMComm> entry : comms.entrySet()){
			int tagInMap = entry.getKey();
			if(tagInMap == tag){
				return comms.get(tag);
			}
		}
		return null;
	}
	
	private final Comparator<GMComm> sorter = new Comparator<GMComm>() {

		public int compare(GMComm o1, GMComm o2) {
			if(o1.getTag() - o2.getTag() <= 0){
				return -1;
			}
			return 1;
		}
	};
	
	public List<GMServer> getGMServers(){
		List<ZZServerInfo> serverInfos = GMClientServer.getInstance().getClientServerConfig().getConnectServers();
//		ConcurrentHashMap<Integer, ServerConfig> serverConfigs = GMClientServer.getInstance().getServerConfigs();
		List<GMServer> gmServers = packGMServers(serverInfos);
		return gmServers;
	}
	
	
	public List<GMServer> getGMServersByPlat(Plat plat){
		List<GMServer> gmServers = getGMServers();
		List<GMServer> resultServers = new ArrayList<GMServer>();
		for(GMServer server : gmServers){
			if(server.getPlat().equals(plat.getPlatName())){
				resultServers.add(server);
			}
		}
		return resultServers;
	}
	
	private List<GMServer> packGMServers(List<ZZServerInfo> serverInfos){
		List<GMServer> gmServers = new ArrayList<GMServer>();
		for(ZZServerInfo si : serverInfos){
			GMServer gmServer = new GMServer();
			gmServer.setServerId(si.getId());
			gmServer.setServerName(si.getName());
			gmServer.setPlat(si.getPlat());
			gmServers.add(gmServer);
		}
		return gmServers;
	}
	
	private List<GMServer> packGMServers(List<ServerInfo> serverInfos, Map<Integer, ServerConfig> serverConfigs){
		List<GMServer> gmServers = new ArrayList<GMServer>();
		for(ServerInfo si : serverInfos){
			ServerConfig config = serverConfigs.get(si.getId());
			if(config == null){
				continue;
			}
			GMServer gmServer = new GMServer();
			gmServer.setServerId(si.getId());
			gmServer.setServerName(config.getName());
			gmServers.add(gmServer);
		}
		return gmServers;
	}
	
	private final Comparator<ServerInfo> serverInfoSorter = new Comparator<ServerInfo>() {

		public int compare(ServerInfo o1, ServerInfo o2) {
			if((o1.getId() - o2.getId()) <= 0){
				return -1;
			}
			return 1;
		}
	};
}
